<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="服务名" prop="service">
      <el-input v-model="dataForm.service" placeholder="服务名"></el-input>
    </el-form-item>
    <el-form-item label="上级服务" prop="superiorService">
      <el-input v-model="dataForm.superiorService" placeholder="上级服务"></el-input>
    </el-form-item>
    <el-form-item label="集群数目" prop="num">
      <el-input v-model="dataForm.num" placeholder="集群数目"></el-input>
    </el-form-item>
    <el-form-item label="开启服务数" prop="opense">
      <el-input v-model="dataForm.opense" placeholder="开启服务数"></el-input>
    </el-form-item>
    <el-form-item label="操作" prop="operation">
      <el-radio-group v-model="dataForm.operation">
          <el-radio :label="0">关闭</el-radio>
          <el-radio :label="1">开启</el-radio>
        </el-radio-group>
      <!-- <el-input v-model="dataForm.operation" placeholder="操作"></el-input> -->
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          service: '',
          superiorService: '',
          num: '',
          opense: '',
          operation: ''
        },
        dataRule: {
          service: [
            { required: true, message: '服务名不能为空', trigger: 'blur' }
          ],
          superiorService: [
            { required: true, message: '上级服务不能为空', trigger: 'blur' }
          ],
          num: [
            { required: true, message: '集群数目不能为空', trigger: 'blur' }
          ],
          opense: [
            { required: true, message: '开启服务数不能为空', trigger: 'blur' }
          ],
          operation: [
            { required: true, message: '操作不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/Service/service/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.service = data.service.service
                this.dataForm.superiorService = data.service.superiorService
                this.dataForm.num = data.service.num
                this.dataForm.opense = data.service.opense
                this.dataForm.operation = data.service.operation
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/Service/service/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'service': this.dataForm.service,
                'superiorService': this.dataForm.superiorService,
                'num': this.dataForm.num,
                'opense': this.dataForm.opense,
                'operation': this.dataForm.operation
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
